class Singleton(type):
    def __init__(cls, name, bases, dict):
        super(Singleton, cls).__init__(name, bases, dict)
        cls._instance = None


    def __call__(cls, *args, **kw):
        if cls._instance is None:
            cls._instance = super(Singleton, cls).__call__(*args, **kw)
        return cls._instance



class PortManager(object):
    __metaclass__ = Singleton
    

    def __init__(self, base_port=10000):
        self.ports = []
        self.base_port = base_port


    def next(self):
        if len(self.ports) == 0:
            return self.base_port
        else:
            return self.ports[-1] + 1


    def assign(self):
        result = self.next()
        self.ports.append(result)
        return result